package neusoft.edu.cn.guanshangshiguang.activities;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.adapter.CommentSchoolAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.CommentSchoolBean;
import neusoft.edu.cn.guanshangshiguang.common.Common;
import neusoft.edu.cn.guanshangshiguang.model.CommentSchoolModel;

public class BorrowDetailsActivity extends AppCompatActivity {

    private List<CommentSchoolBean> commentSchoolBean;
    CommentSchoolAdapter commentSchoolAdapter;
    private ImageView bpic;
    private TextView bname,borrow_time,jianjie;
    private Button xujie,pay;
    private int shuaxin_flag=0;
    private RecyclerView contentRecycler;
    SwipeRefreshLayout swipeRefreshLayout;

    ListListener listListener = new ListListener() {
        @Override
        public void onResponse(List List) {
            commentSchoolBean = List;
            commentSchoolAdapter.setDate(commentSchoolBean);
            if(shuaxin_flag==1){
                shuaxin_flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_borrow_details);

        init();
        initRecycler();

        Picasso.get()
                .load(Common.Address+getIntent().getStringExtra("bpic"))
                    .into(bpic);
        bname.setText(getIntent().getStringExtra("bname"));
        bname.setTextSize(18);
        borrow_time.setText("借阅时间："+getIntent().getStringExtra("borrow_time"));
        jianjie.setText("简介："+getIntent().getStringExtra("intro"));

        CommentSchoolModel commentSchoolModel = new CommentSchoolModel();
        commentSchoolModel.getResultList(getIntent().getStringExtra("school_bid"),listListener);
    }

    private void init() {
        bname=findViewById(R.id.bname);
        borrow_time=findViewById(R.id.borrow_time);
        jianjie=findViewById(R.id.borrow_jianjie);
        bpic=findViewById(R.id.bpic);
        xujie=findViewById(R.id.xujie);
        pay=findViewById(R.id.pay);
    }
    private void initRecycler() {
        commentSchoolAdapter = new CommentSchoolAdapter();

        contentRecycler = findViewById(R.id.contentList);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        contentRecycler.setHasFixedSize(true);

        swipeRefreshLayout = findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setRefreshing(false);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuaxin_flag=1;
                CommentSchoolModel commentSchoolModel = new CommentSchoolModel();
                commentSchoolModel.getResultList(getIntent().getStringExtra("school_bid"),listListener);
            }
        });

        contentRecycler.setLayoutManager(linearLayoutManager);
        contentRecycler.setAdapter(commentSchoolAdapter);

    }
}
